package org.dromara.third.convert.splz.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.WorkApprovalFlow;
import org.dromara.system.mapper.WorkApprovalFlowMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.splz.dto.SplzDTO;
import org.dromara.third.convert.splz.validator.SplzValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertSplzController {

    @Autowired
    private WorkApprovalFlowMapper workApprovalFlowMapper;

    @PostMapping("/splz")
    @Transactional
    public R<Void> addSplz(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        SplzDTO splzDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            splzDTO = JSONObject.parseObject(decrypt, SplzDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, splzDTO, new SplzValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        WorkApprovalFlow workApprovalFlow = assembleSplzData(orgCode, splzDTO);
        int count = workApprovalFlowMapper.countWithOutDelFlag(workApprovalFlow.getId());
        if (count == 0) {
            workApprovalFlowMapper.insert(workApprovalFlow);
        } else {
            workApprovalFlowMapper.updateWithOutDelFlag(workApprovalFlow);
        }
        return R.ok();
    }

    private WorkApprovalFlow assembleSplzData(String orgCode, SplzDTO dto) {
        WorkApprovalFlow workApprovalFlow = new WorkApprovalFlow();
        try {
            workApprovalFlow.setId(dto.getDataId());
            workApprovalFlow.setZyid(dto.getZyid());
            workApprovalFlow.setZylx(dto.getZylx());
            workApprovalFlow.setLcjd(dto.getLcjd());
            workApprovalFlow.setClry(dto.getClry());
            workApprovalFlow.setClyj(dto.getClyj());
            workApprovalFlow.setClsj(DateUtils.parseDate(dto.getClsj()));
            workApprovalFlow.setHzclqk(dto.getHzclqk());
            workApprovalFlow.setQmtp(dto.getQmtp());
            workApprovalFlow.setQmtp(dto.getQmtp());
            workApprovalFlow.setDelFlag(Long.parseLong(dto.getDelFlag()));
            workApprovalFlow.setCreateDept(Long.parseLong(dto.getCreateDept()));
            workApprovalFlow.setCreateBy(Long.parseLong(dto.getCreateBy()));
            workApprovalFlow.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            workApprovalFlow.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            workApprovalFlow.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            workApprovalFlow.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装作业审批流转记录信息异常");
        }
        return workApprovalFlow;
    }
}
